// app.js
const express = require('express');
const cors = require('cors');
const api = require('./api');
const app = express();
const PORT = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});
app.use('/uploads', express.static('uploads'));

app.use('/api', api);

// 错误拦截
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).json({ message: '内部服务器错误' });
});

app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});